package jwdt.xadmin.added.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.added.bean.TImages;
import jwdt.xadmin.added.service.ImageManageService;

import com.ezerg.db.ext.PageUtil;
import com.ezerg.image.ImageInfo;
import com.ezerg.image.ImageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.web.ServletActionContext;
import com.ezerg.web.cfg.WebConfig;
import com.ezerg.web.fileupload.FileItem;

public class ImageManageServiceImpl implements ImageManageService{
	private BaseDao<TImages> baseDao;
	private int imageZoom = WebConfig.get().getImageZoomSize();
	public void setBaseDao(BaseDao<TImages> baseDao) {
		this.baseDao = baseDao;
	}
	@Override
	public PageUtil doList(PageUtil pu) throws Exception{
		String sql = " order by id asc";
		pu.doList(TImages.class,sql);
		//创造json数据对象
		return pu;
	}
	@Override
	public String doAddact(TImages obj,FileItem file) throws Exception {
		ServletContext sc = ServletActionContext.getServletContext();
		String basePath = sc.getRealPath("/");
		String uploadPath = basePath.replace("\\", "/") + "uploadfile/";
		File up = new File(uploadPath + "media/");
		try {
			if(!up.exists()) {
				up.mkdirs();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String ret = "添加成功";
		try {
			if(file != null) {
				String fileExt = file.getFileExt();
				String fileName = DateUtil.getComplexDateTime() + "." + fileExt;
				obj.setPath(fileName);
				String absolutePath = uploadPath + fileName;
				//
				ImageInfo ii = new ImageInfo();
				ii.setInput(file.getInputStream());
				if(ii.check() && imageZoom>0) {//图片
					//System.out.println("Image Type=" + ii.getFormatName());
					try {// 图片缩放功能
						BufferedImage image = ImageUtil.zoom(file.getInputStream(), imageZoom, imageZoom);
						ImageIO.write(image, ii.getFormatName(), new FileOutputStream(absolutePath));
					} catch (Exception e) {
						e.printStackTrace();
						// 缩放失败则直接定入
						file.write(absolutePath);
						BufferedImage image = ImageUtil.zoom(absolutePath, imageZoom, imageZoom);
						ImageIO.write(image, ii.getFormatName(), new FileOutputStream(absolutePath));
					}
				} else {
					ret = "请上传图片文件";
				}
			}else{
				ret = "请上传图片文件";
			}
			obj.setDt(DateUtil.getDateTime());
			ret = baseDao.doAddObj(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		return ret;
	}

	@Override
	public String doEditact(TImages obj, int id,FileItem file) throws Exception {
		ServletContext sc = ServletActionContext.getServletContext();
		String basePath = sc.getRealPath("/");
		String uploadPath = basePath.replace("\\", "/") + "uploadfile/";
		File up = new File(uploadPath + "media/");
		String ret = "修改成功";
		try {
			if(!up.exists()) {
				up.mkdirs();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TImages i = new TImages();
		i.setId(obj.getId());
		i.setPosition(obj.getPosition());
		i.setTitle(obj.getTitle());
		i.setUrl(obj.getUrl());
		i.setDt(DateUtil.getDateTime());
		i.setZt(obj.getZt());
		try {
			if(file != null) {
				//
				ImageInfo ii = new ImageInfo();
				ii.setInput(file.getInputStream());
				if(ii.check()) {//图片
					String fileExt = file.getFileExt();
					String fileName = DateUtil.getComplexDateTime() + "." + fileExt;
					i.setPath(fileName);
					String absolutePath = uploadPath + fileName;
					//System.out.println("Image Type=" + ii.getFormatName());
					if(imageZoom > 0) {
						try {// 图片缩放功能
							BufferedImage image = ImageUtil.zoom(file.getInputStream(), imageZoom, imageZoom);
							ImageIO.write(image, ii.getFormatName(), new FileOutputStream(absolutePath));
						} catch (Exception e) {
							e.printStackTrace();
							// 缩放失败则直接定入
							file.write(absolutePath);
							BufferedImage image = ImageUtil.zoom(absolutePath, imageZoom, imageZoom);
							ImageIO.write(image, ii.getFormatName(), new FileOutputStream(absolutePath));
						}
					} else {
						file.write(absolutePath);
					}
				}
			}
			ret = baseDao.doUpdateObj(TImages.class, id, i);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改出错";
		}
		return ret;
	};
	
	@Override
	public String doDelact(int id) throws Exception {
		return baseDao.doDelObj(TImages.class, id);
	}

	@Override
	public TImages doLoad(int id) throws Exception {
		return baseDao.doLoadObj(TImages.class, id);
	}
	@Override
	public String doUpdateState(int id, String result) throws Exception {
		return baseDao.doIntUpdateState(TImages.class, "id="+id, result);
	}
}
